<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	var userId = "";
	var taskId = "";
	/* 科技处添加用户页面  */
	$("button[data-type='save-button']").on("click",function(e){
		$("#opinion").html("");
		$("#myTable").html("");
		taskId = $(this).attr("data-taskid");
		userId = $(this).attr("data-userid");
		$.get("${path}/ti/activiti/saveUser",function(data,status){
	    	if(data.success){
	    		var users = data.message;
	    		$.each(users, function (index) {
	    		  var rowTr = document.createElement('div')
		          //找到html的tr节点
	              var child = "<input type='checkbox' name='test' value='"+ users[index].userId +"'>" + users[index].userName + "--" + users[index].userId +"<br/>"
	               //将要展示的信息写入页面
	               rowTr.innerHTML = child;
	               $("#myTable").append(rowTr);//将信息追加
	    		});
	    		$("#myTable").append("<input type='text' name='opinion'><br/>");
	    		$("#myTable").append("<button type='button' id='saveBut'>audit</button>");
	    	}
	  	});
	});
	
	/* 添加审核意见页面 */
	$("button[data-type='audit-button']").on("click",function(e){
		$("#myTable").html("");
		$("#opinion").html("");
		taskId = $(this).attr("data-taskid");
		userId = $(this).attr("data-userid");
		var rowTr = document.createElement('div')
        //找到html的tr节点
        var child = "<input type='text' name='opinion'><br/> <button type='button' id='saveOpinion'>audit</button>"
          //将要展示的信息写入页面
        rowTr.innerHTML = child;
        $("#opinion").append(rowTr);//将信息追加
	});
	
	/* 审核 */
	$("#opinion").on("click", "#saveOpinion", function () {
		var opinion = $("input[name='opinion']").val();
		$.post("${path}/ti/activiti/audit",{taskId : taskId,state:1,userId:userId,opinion:opinion},function(data,status){
	    	if(data.success){
	    		location.reload();
	    	}
  		});
	});
	
	/* 退回 */
	$("button[data-type='reject-button']").on("click",function(e){
		var taskId = $(this).attr("data-taskid");
		var usertaskId = $(this).attr("data-usertaskId");
		$.post("${path}/ti/activiti/taskRoll",{taskId : taskId,usertaskId:usertaskId},function(data,status){
	    	if(data.success){
	    		location.reload();
	    	}
	  	});
	});
	
	/* 撤回 */
	$("button[data-type='withdraw-button']").on("click",function(e){
		var taskId = $(this).attr("data-taskid");
		var usertaskId = $(this).attr("data-usertaskId");
		$.post("${path}/ti/activiti/taskRoll",{taskId : taskId,usertaskId:usertaskId},function(data,status){
	    	if(data.success){
	    		location.reload();
	    	}
	  	});
	});
	
	/* 添加用户 */
	$("#myTable").on("click", "#saveBut", function () {
		var obj=document.getElementsByName('test'); //选择所有name="'test'"的对象，返回数组 
		var opinion = $("input[name='opinion']").val();
		//取到对象数组后，我们来循环检测它是不是被选中 
		var s=''; 
		for(var i=0; i<obj.length; i++){ 
			if(obj[i].checked){
				s+=obj[i].value+','; //如果选中，将value添加到变量s中 
			}
		} 
		if(s!=''){
			/*审核*/
			$.post("${path}/ti/activiti/audit",{taskId : taskId,state:1,users:s,userId:userId,opinion:opinion},function(data,status){
		    	if(data.success){
		    		location.reload();
		    	}
		  	});
		}
	})
});

</script>
<div>
	<form id="auditForm" action="${path}/ti/activiti/queryTask" method="get" >
		<input name="userId"/> 
		<button type="submit"> 查询任务</button>
	</form>
	#if( $list )
	   <span>待审核任务</span><br/>
		#foreach ($item in $list)
			<div>
				$!{velocityCount}： 审核环节：${item.taskName} -- 立项标题：${item.title} -- 立项内容：${item.context}<br/>
				${item.lcslId} -- 立项状态：${item.state}<br/>
				#foreach ($it in $item.opinion)
					审核人：${it.USERID} -- 审核意见：${it.OPINION}
				#end
				<button type='button' data-type='audit-button' data-taskid='${item.taskId}' data-userid="${item.userId}">审核任务</button> 
				<button type='button' data-type='save-button' data-taskid='${item.taskId}' data-userid="${item.userId}">添加专家</button> 
				<button type='button' data-type='reject-button' data-taskid='${item.taskId}' data-usertaskId="usertask1">驳回到项目方</button>
				<button type='button' data-type='withdraw-button' data-taskid='${item.taskId}' data-usertaskId="usertask2">撤回</button><br/>
			</div>
		#end
	#end
	<!-- 添加审核专家 -->
	<div id="myTable"> 
	</div>
	<div id="opinion"></div>
</div>